package WebLog;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class LoggerMapper extends Mapper<LongWritable, Text,Text, NullWritable> {
    //封装数据
    Text k =new Text();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //获取一行数据：194.237.142.21 - - [18/Sep/2013:06:49:18 +0000] "GET /wp-content/uploads/2013/07/rstudio-git3.png HTTP/1.1" 304 0 "-" "Mozilla/4.0 (compatible;)"
        String line = value.toString();

        //创建日志解析（分割）方法
        String [] fields =parseLog(line);
        String data ="";
        //封装数据(遍历数组)
        if(fields[0] != null){
            for(String field: fields ){
                data=data+field+"\t";
            }
            k.set(data);
            //写出
            context.write(k,NullWritable.get());
        }else {

            return;
        }


    }

    /**
     * 实现具体的解析方法
     * @param line
     * @return
     */
       //实现分割方法
    private String[] parseLog(String line) {
        //截取
        String[] fields =line.split(" ");
        //获取所需的字段
          //状态码400:183.49.46.228 - - [18/Sep/2013:06:49:23 +0000] "-" 400 0 "-" "-"
       // String StatusCode = fields[5];
        //判断是否是正常的字段
        if(fields.length>11){//正常
            String ip = fields[0];
            String date = fields[3];
            String httpType=fields[5];
          /*  //浏览器信息
            String agent = fields[fields.length-1];*/
            return  new String []{ip,date,httpType};
        }else{//不正常
            return new String[]{null,null,null};
        }

    }
}
